In [169]:
my_cars = ['BMW 320', 'Benz C class', 'Porshe', 'Maruthi 800']
In [170]:
len(my_cars)
Out[170]:
In [171]:
print my_cars[0]
In [172]:
print my_cars[1]
In [173]:
print my_cars[10000]
In [174]:
my_favourites = my_cars
In [175]:
type(my_cars)
Out[175]:
In [176]:
my_favourites.append("Tesla Model S")
In [177]:
my_cars
Out[177]:
In [178]:
awesome_people = ["T.R", "Vijaykanth"]
In [179]:
awesome_cars = ["BMW", "Merc"]
In [180]:
awesome_things = awesome_people + awesome_cars
In [181]:
awesome_things
Out[181]:
In [182]:
awesome_cars + awesome_people
Out[182]:
In [183]:
for car in my_cars:
print car, "is my car!"
Note: Do NOT add or remove an element during iteration
In [184]:
def cube(x):
return x*x*x
nums = [1,2,3,4,5,6]
cubed_nums = []
for num in nums:
cubed_nums.append(cube(num))
print cubed_nums
In [185]:
"BMW 320" in my_cars
Out[185]:
In [187]:
if "BMW 320" in my_cars:
print "i am a rich man"
In [186]:
"Maruthi Zen" in my_cars
Out[186]:
In [188]:
a_string = "Hello World"
for every_character in a_string:
print "[" + every_character + "]"
In [189]:
long_string = "The quick brown fox jumps over the lazy dog"
In [190]:
long_string.split(" ")
Out[190]:
In [191]:
long_string.title()
Out[191]:
In [194]:
cap_words = []
for word in long_string.split():
cap_words.append(word.capitalize())
cap_words
Out[194]:
In [196]:
"_".join(cap_words)
Out[196]:
In [197]:
" ".join([1,2,3])
In [198]:
range(10)
Out[198]:
In [200]:
for i in range(10):
print i
In [201]:
range(5, 10)
Out[201]:
In [202]:
range(5, 10, 2)
Out[202]:
In [203]:
for num in range(10):
print cube(num)
In [204]:
i=0
while i < len(my_cars):
print my_cars[i]
i += 2
In [ ]:
my_cars
In [205]:
my_cars = []
In [206]:
my_cars
Out[206]:
In [207]:
my_cars.append("BMW 320")
In [208]:
my_cars.append("Tesla Model S")
In [209]:
my_cars
Out[209]:
Note: Doesn't return a new list, modifies the original
In [210]:
my_cars
Out[210]:
In [211]:
my_cars.insert(0, "Merc C Class")
In [212]:
my_cars
Out[212]:
In [213]:
his_cars = ["Maruthi 800"]
In [214]:
my_cars
Out[214]:
In [215]:
my_cars.extend(his_cars)
In [216]:
my_cars
Out[216]:
In [217]:
a = [1,2]
In [218]:
b = [3,4]
In [219]:
a.append(b)
In [220]:
a
Out[220]:
In [221]:
my_cars.index("BMW 320")
Out[221]:
In [222]:
my_cars[1]
Out[222]:
In [223]:
my_cars.index("Porshe")
In [224]:
my_cars.remove("BMW 320")
In [225]:
my_cars
Out[225]:
In [226]:
my_cars.remove("BMW 320")
In [227]:
my_cars
Out[227]:
In [228]:
my_cars.sort()
In [229]:
my_cars
Out[229]:
In [230]:
my_cars
Out[230]:
In [231]:
my_cars.reverse()
In [232]:
my_cars
Out[232]:
In [233]:
my_cars
Out[233]:
In [234]:
my_cars.pop()
Out[234]:
In [235]:
my_cars
Out[235]:
In [236]:
my_cars.pop(0)
Out[236]:
In [237]:
my_cars
Out[237]:
Note: All of the above methods work IN-PLACE, meaning that they don't return any result
In [238]:
till_100 = range(100)
In [239]:
till_100
Out[239]:
In [240]:
till_100[:5]
Out[240]:
In [241]:
till_100[-5:]
Out[241]:
In [242]:
till_100[0] = "Booga"
In [250]:
for i in till_100[:5]:
print type(i) is int
In [247]:
help(instance of)
In [252]:
isinstance("hello", str)
Out[252]:
In [245]:
till_100[:5]
Out[245]:
A
and B
of basic/list1.py